package Pages.CourseChoose;

import JDBC.DBUtil;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ResourceBundle;

import static Data.ClassJTable.fillTable;

/**
 * description:
 * author: 柳胜酉
 * 2022/7/12
 */
public class SCourseChoosePage extends JFrame {

    private JPanel dialogPane;
    private JPanel contentPanel;
    private JPanel panel1;
    private JPanel panel2;
    private JLabel label1;
    private JPanel panel3;
    private JLabel label2;
    private JComboBox comboBox1;
    private JComboBox comboBox2;
    private JPanel panel4;
    private JLabel label3;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JPanel buttonBar;
    private JButton okButton;
    private JButton cancelButton;
    private DefaultTableModel dtm = null;
    private String table = "user_course_f";
    private String s_teacher = null;
    private String s_semester = null;
    String[] s1 = null;
    String[][] s2 = null;
    String sNo = null;
    String sCourse = null;
    public SCourseChoosePage() {
        initComponents();
    }

    private void initComponents() {
        ResourceBundle bundle = ResourceBundle.getBundle("P0704");
        dialogPane = new JPanel();
        contentPanel = new JPanel();
        panel1 = new JPanel();
        panel2 = new JPanel();
        label1 = new JLabel();
        panel3 = new JPanel();
        label2 = new JLabel();
        s1 = new String[]{"公选课", "选修课"};
        s2 = new String[][]{{"----课程----", "数学", "体育", "英语", "Java", "C++"}, {"----课程----", "神奇宇宙", "生物进化论","Python程序","茶道","安卓应用"}};
        comboBox1 = new JComboBox(s1);
        comboBox2 = new JComboBox(s2[0]);
        panel4 = new JPanel();
        label3 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        buttonBar = new JPanel();
        okButton = new JButton();
        cancelButton = new JButton();
        s_semester = "2021-2022第一学期";
        //s_teacher = "牛牛";

        String[] head = {"课程", "年级", "教师", "学期"};
        dtm = new DefaultTableModel();
        //遍历数组，确定你有几列
        for (int i = 0; i < head.length; i++) {
            dtm.addColumn(head[i]);
        }
        fillTable(dtm, table);//表格内容的填充
        table1 = new JTable(dtm);
        scrollPane1 = new JScrollPane(table1);

        //======== this ========
        setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("添加课程");
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== dialogPane ========
        {
            dialogPane.setBorder(new EmptyBorder(12, 12, 12, 12));
            dialogPane.setBackground(SystemColor.inactiveCaption);
            dialogPane.setLayout(new BorderLayout());

            //======== contentPanel ========
            {
                contentPanel.setLayout(new CardLayout());

                //======== panel1 ========
                {
                    panel1.setBackground(SystemColor.inactiveCaption);
                    panel1.setLayout(new BorderLayout());

                    //======== panel2 ========
                    {
                        panel2.setBackground(SystemColor.activeCaption);
                        panel2.setLayout(new GridLayout(2, 1));

                        //---- label1 ----
                        label1.setText("添加课程信息");
                        label1.setHorizontalAlignment(SwingConstants.CENTER);
                        label1.setFont(label1.getFont().deriveFont(label1.getFont().getSize() + 20f));
                        label1.setBackground(SystemColor.inactiveCaption);
                        label1.setForeground(Color.white);
                        panel2.add(label1);

                        //======== panel3 ========
                        {
                            panel3.setBackground(SystemColor.inactiveCaption);
                            panel3.setLayout(new GridLayout(1, 3));

                            //---- label2 ----
                            label2.setText(bundle.getString("CourseSelectPage.label2.text_2"));
                            label2.setHorizontalAlignment(SwingConstants.CENTER);
                            label2.setFont(label2.getFont().deriveFont(label2.getFont().getSize() + 15f));
                            label2.setForeground(Color.darkGray);
                            label2.setIcon(new ImageIcon(getClass().getResource("/image/xinrenkecheng.png")));
                            panel3.add(label2);

                            //---- comboBox1 ----
                            comboBox1.setFont(comboBox1.getFont().deriveFont(comboBox1.getFont().getSize() + 5f));
                            panel3.add(comboBox1);

                            //---- comboBox2 ----
                            comboBox2.setFont(comboBox2.getFont().deriveFont(comboBox2.getFont().getSize() + 5f));
                            panel3.add(comboBox2);
                        }
                        panel2.add(panel3);
                    }
                    panel1.add(panel2, BorderLayout.NORTH);

                    //======== panel4 ========
                    {
                        panel4.setBackground(SystemColor.inactiveCaption);
                        panel4.setLayout(new BorderLayout());

                        //---- label3 ----
                        label3.setText(bundle.getString("CourseSelectPage.label3.text_2"));
                        label3.setFont(label3.getFont().deriveFont(label3.getFont().getSize() + 5f));
                        label3.setForeground(Color.darkGray);
                        panel4.add(label3, BorderLayout.NORTH);

                        //======== scrollPane1 ========
                        {
                            scrollPane1.setViewportView(table1);
                        }
                        panel4.add(scrollPane1, BorderLayout.CENTER);
                    }
                    panel1.add(panel4, BorderLayout.CENTER);
                }
                contentPanel.add(panel1, "card1");
            }
            dialogPane.add(contentPanel, BorderLayout.CENTER);

            //======== buttonBar ========
            {
                buttonBar.setBorder(new EmptyBorder(12, 0, 0, 0));
                buttonBar.setBackground(SystemColor.inactiveCaption);
                buttonBar.setLayout(new GridBagLayout());
                ((GridBagLayout)buttonBar.getLayout()).columnWidths = new int[] {0, 85, 80};
                ((GridBagLayout)buttonBar.getLayout()).columnWeights = new double[] {1.0, 0.0, 0.0};

                //---- okButton ----
                okButton.setText("添加");
                okButton.setFont(okButton.getFont().deriveFont(okButton.getFont().getSize() + 10f));
                buttonBar.add(okButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 5), 0, 0));

                //---- cancelButton ----
                cancelButton.setText(bundle.getString("CourseSelectPage.cancelButton.text_2"));
                cancelButton.setFont(cancelButton.getFont().deriveFont(cancelButton.getFont().getSize() + 10f));
                buttonBar.add(cancelButton, new GridBagConstraints(2, 0, 1, 1, 0.0, 0.0,
                        GridBagConstraints.CENTER, GridBagConstraints.BOTH,
                        new Insets(0, 0, 0, 0), 0, 0));
            }
            dialogPane.add(buttonBar, BorderLayout.SOUTH);
        }
        contentPane.add(dialogPane, BorderLayout.CENTER);
        setSize(380, 360);
        setLocationRelativeTo(null);
        Item i = new Item();
        comboBox1.addItemListener(myitem);

        comboBox1.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.SELECTED == e.getStateChange()) {
                    System.out.println("你选择了：" + comboBox1.getSelectedItem() + "是第" + (comboBox1.getSelectedIndex() + 1) + "个选项");
//                    if (comboBox1.getSelectedIndex() == 0) {
//                        table = "user_course_public";
//                    } else if (comboBox1.getSelectedIndex() == 1) {
//                        table = "user_course_self";
//                    }
                    //System.out.println(table);
                }
            }
        });
        //System.out.println(table);


        comboBox2.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                if (e.SELECTED == e.getStateChange()) {
                    System.out.println("你选择了：" + comboBox2.getSelectedItem() + "是第" + (comboBox2.getSelectedIndex() + 1) + "个选项");
                    if (comboBox1.getSelectedIndex() == 0) {
                        if (comboBox2.getSelectedIndex() == 1) {
                            sCourse = "数学";
                            s_teacher = "牛牛";
                        } else if (comboBox2.getSelectedIndex() == 2) {
                            sCourse = "体育";
                            s_teacher = "露露";
                        } else if (comboBox2.getSelectedIndex() == 3) {
                            sCourse = "英语";
                            s_teacher = "唔唔";
                        } else if (comboBox2.getSelectedIndex() == 4) {
                            sCourse = "Java";
                            s_teacher = "美美";
                        } else if (comboBox2.getSelectedIndex() == 5) {
                            sCourse = "C++";
                            s_teacher = "嘟嘟";
                        }
                    } else if (comboBox1.getSelectedIndex() == 1) {
                        if (comboBox2.getSelectedIndex() == 1) {
                            sCourse = "神奇宇宙";
                            s_teacher = "舒舒";
                        } else if (comboBox2.getSelectedIndex() == 2) {
                            sCourse = "生物进化论";
                            s_teacher = "谷谷";
                        } else if (comboBox2.getSelectedIndex() == 3) {
                            sCourse = "Python程序";
                            s_teacher = "酷酷";
                        } else if (comboBox2.getSelectedIndex() == 4) {
                            sCourse = "茶道";
                            s_teacher = "虎虎";
                        } else if (comboBox2.getSelectedIndex() == 5) {
                            sCourse = "安卓应用";
                            s_teacher = "醋醋";
                        }
                    }
                    System.out.println(table + " " + sCourse);
                }
            }
        });

//        okButton.addActionListener(new ActionListener() {
//            @Override
//            public void actionPerformed(ActionEvent e) {
//                int flag = JOptionPane.showConfirmDialog(null, "确认添加？", "修改提示", JOptionPane.YES_NO_OPTION);
//                if (flag == JOptionPane.YES_OPTION) {
//                    if (table == "user_course_public") {
//                        if (DBUtil.insertDataOfCourse(table, sNo, sCourse)) {
//                            JOptionPane.showMessageDialog(null, "添加成功！", "添加提示", JOptionPane.INFORMATION_MESSAGE);
//                            dtm.setRowCount(0);
//                            table1.updateUI();
//                            fillTable(dtm, table);
//                        } else {
//                            JOptionPane.showMessageDialog(null, "添加失败！", "添加提示", JOptionPane.INFORMATION_MESSAGE);
//                        }
//                    } else if (table == "user_course_self") {
//                        if (DBUtil.insertDataOfCourse(table, sNo, sCourse)) {
//                            JOptionPane.showMessageDialog(null, "添加成功！", "添加提示", JOptionPane.INFORMATION_MESSAGE);
//                            dtm.setRowCount(0);
//                            table1.updateUI();
//                            fillTable(dtm, table);
//                        } else {
//                            JOptionPane.showMessageDialog(null, "添加失败！", "添加提示", JOptionPane.INFORMATION_MESSAGE);
//                        }
//                    }
//                }
//            }
//        });
        okButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int flag = JOptionPane.showConfirmDialog(null, "确认添加？", "修改提示", JOptionPane.YES_NO_OPTION);
                if (flag == JOptionPane.YES_OPTION) {
                    if (DBUtil.insertDataOfFake(table ,sCourse,s_teacher,s_semester)) {
                        JOptionPane.showMessageDialog(null, "添加成功！", "添加提示", JOptionPane.INFORMATION_MESSAGE);
                        dtm.setRowCount(0);
                        table1.updateUI();
                        fillTable(dtm, table);
                    } else {
                        JOptionPane.showMessageDialog(null, "添加失败！", "添加提示", JOptionPane.INFORMATION_MESSAGE);
                    }
                }
            }
        });

        cancelButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                dispose();
            }
        });

    }

    //定义匿名类实现监听
    ItemListener myitem = new ItemListener() {
        //完成下拉列表框事件监听
        public void itemStateChanged(ItemEvent e) {
            //确定课程类型选定内容是什么，
            //从二位数组中取出对应课程，
            int index = comboBox1.getSelectedIndex();
            comboBox2.removeAllItems();//清空城市下拉框内容
            for (int i = 0; i < 2; i++) {
                if (i == index) {

                    for (int j = 0; j < s2[index].length; j++) {
                        comboBox2.addItem(s2[index][j]);
                    }
                }
            }
        }
    };

    public class Item implements ItemListener {
        public void itemStateChanged(ItemEvent e) {
            int index = comboBox1.getSelectedIndex();
            comboBox2.removeAllItems();//清空城市下拉框内容
            for (int i = 0; i < 2; i++) {
                if (i == index) {
                    for (int j = 0; j < s2[index].length; j++) {
                        comboBox2.addItem(s2[index][j]);
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        new SCourseChoosePage().setVisible(true);
    }
}
